const {resolve} = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const WorkboWebpackPlugin = require('workbox-webpack-plugin')

/*
    pwa:渐进式网络开发应用程序（离线可以访问）
        workbox --> workbox-webpack-plugin
 */

// 定义nodejs环境变量：决定使用browserslist的哪个环境
process.env.NODE_ENV = 'development'

// 复用loader
const commonCssLoader = [
    MiniCssExtractPlugin.loader,
    'css-loader',
    {
        // 还需要在pack.json中定义browserslist
        loader: 'postcss-loader',
        options: {
            ident: 'postcss',
            plugins: () => [
                require('postcss-preset-env')()
            ]
        }
    }
]

module.exports = {
    entry: './src/js/index.js',

    output: {
        filename: 'js/built.[hash:10].js',
        path: resolve(__dirname, 'build')
    },

    module: {
        rules: [
            /*
               正常来讲，一个文件只能被一个loader处理。
               当一个文件要被多个loader处理，那么一定要指定loader的执行先后顺序：
               先执行eslint   再执行babel
            */

            {
                // 在package.json中eslintConfig --> airbnb
                test: /\.js$/,
                exclude: /node_modules/,
                // 优先执行
                enfore: 'pre',
                loader: 'eslint-loader',
                options: {
                    fix: true
                }
            },

            {
                /*
                    以下loader只会匹配一个
                    注意：不能有两个配置处理同一种类型文件
                 */
                oneOf: [
                    {
                        test: /\.css$/,
                        use: [
                            /*'style-loader',*/
                            ...commonCssLoader
                        ]
                    },

                    {
                        test: /\.less$/,
                        use: [
                            /*'style-loader',*/
                            ...commonCssLoader,
                            'less-loader'
                        ]
                    },

                    {
                        test: /\.js$/,
                        exclude: /node_modules/,
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    '@babel/preset-env',
                                    {
                                        useBuiltIns: 'usage',
                                        corejs: {
                                            version: 3
                                        },
                                        targets: {
                                            chrome: '60',
                                            firefox: '60'
                                        }
                                    }
                                ]
                            ],
                            /*
                                开启Babel缓存
                                第二次构建时，会读取之前的缓存
                             */
                            cacheDirectory: true
                        }
                    },

                    {
                        test: /\.(jpg|png|gif)$/,
                        loader: 'url-loader',
                        options: {
                            limit: 8 * 1024,
                            name: '[hash:10].[ext]',
                            outputPath: 'imgs',
                            esModule: false
                        }
                    },

                    {
                        test: /\.html$/,
                        loader: 'html-loader',
                        options: {
                            esModule: false
                        }
                    },

                    {
                        exclude: /\.(js|less|css|html|jpg|png|gif)$/,
                        loader: 'file-loader',
                        outputPath: 'media'
                    }
                ]
            }
        ]
    },

    plugins: [
        new MiniCssExtractPlugin({
            filename: 'css/built.[hash:10].css'
        }),

        new OptimizeCssAssetsWebpackPlugin(),

        new HtmlWebpackPlugin({
            template: './src/index.js',
            minify: {
                collapseWhitespace: true,
                removeComments: true
            }
        }),

        new WorkboWebpackPlugin.GenerateSW({
            /*
                1.帮助serviceworker快速启动
                2.删除旧的 serviceworker

                生成一个 serviceworker 配置文件
             */
            clientsClaim: true,
            skipWaiting: true
        })


    ],

    mode: 'production',

    devtool: 'source-map',
}